package dancrawler.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.plaf.PanelUI;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.text.AttributeSet;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class MainWindow extends JFrame implements GameListener, WindowListener, ActionListener {

  @Override
  public void windowOpened(WindowEvent e) {
    gp.requestFocusInWindow();
  }

  @Override
  public void windowClosing(WindowEvent e) {
  }

  @Override
  public void windowClosed(WindowEvent e) {
  }

  @Override
  public void windowIconified(WindowEvent e) {
  }

  @Override
  public void windowDeiconified(WindowEvent e) {
  }

  @Override
  public void windowActivated(WindowEvent e) {
  }

  @Override
  public void windowDeactivated(WindowEvent e) {
  }

  @Override
  public void chatMsg(String player, String msg) {
    if (msg.trim().length() > 0) {
      chatOut.setText(chatOut.getText() + "\n<" + player.trim() + "> " + msg.replace("<", "ILLEGAL CHAR! ").replace(">", " ILLEGAL CHAR! "));
      chatOut.repaint();
    }
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == bChatSend) {
      client.sendChat(chatIn.getText());
      chatIn.setText("");
    }
  }

    @Override
    public void die() {
        JOptionPane.showMessageDialog(this, "YOU ARE DEAD");
    }

    @Override
    public void setViewRange(int range) {
    }

  private class FullScreenAction extends AbstractAction {

    @Override
    public void actionPerformed(ActionEvent e) {
      if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {

        setExtendedState(JFrame.NORMAL);
      } else {
        setExtendedState(JFrame.MAXIMIZED_BOTH);
      }
    }
  }
  GamePanel gp;
  boolean fullscreen;
  private JPanel rpanel = new JPanel();
  private JPanel lpanel = new JPanel();
  private JList plist = new JList(new DefaultListModel());
  private JTextPane chatOut = new JTextPane();
  private JTextPane stats = new JTextPane();
  private JTextArea chatIn = new JTextArea(3, 10);
  private JButton bChatSend = new JButton("Send");
  private Client client;

  public MainWindow(Client client) throws UnsupportedEncodingException {
    super();

    ImageLoader loader = new ImageLoader();
    Image icon = loader.load("/res/img/danborgeDW.png");
    this.setIconImage(icon);
    this.setTitle("DanCrawler version 1.0 - "
            +"by Team 4 - Dan Børge | Eskild Andre Kittelsen, "
            +"Stian Gjevik, Jon Severin Eivik Jakobsen");
    this.setBackground(Color.black);
    setSize(1024, 768);
    this.client = client;
    fullscreen = false;
    //       setExtendedState(JFrame.MAXIMIZED_BOTH);
    //       setUndecorated(true); // Maximized full screen
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.addWindowListener(this);
    BorderLayout layout = new BorderLayout();
    //chatOut.setContentType("text/html");

    stats.setBackground(Color.black);
    stats.setForeground(Color.green);
    stats.setBorder(null);
    chatOut.setBackground(Color.black);
    chatOut.setForeground(Color.green);

    chatIn.setBackground(Color.black);
    chatIn.setForeground(Color.green);

    chatIn.setBorder(null);
    chatOut.setBorder(null);

    chatIn.setSize(200, 400);

    JScrollPane scpo = new JScrollPane(chatOut);
    JScrollPane scpi = new JScrollPane(chatIn);
    JScrollPane scpStat = new JScrollPane(stats);

    scpi.setSize(200, 400);

    System.out.println(scpi.getBackground());
    Dimension d = new Dimension(200, 300);
    bChatSend.addActionListener(this);
    this.setLayout(layout);
    lpanel.setBackground(Color.black);
    rpanel.setBackground(Color.black);


    rpanel.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 0, Color.black));
    lpanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 5, Color.black));

    JLabel[] labels = new JLabel[3];
    labels[0] = new JLabel("PLAYERS", JLabel.CENTER);
    labels[1] = new JLabel("CHAT", JLabel.CENTER);
    labels[2] = new JLabel("STATS", JLabel.CENTER);
    lpanel.add(labels[0]);
    rpanel.add(labels[1]);
    lpanel.add(plist);
    //lpanel.add(labels[2]);
    //lpanel.add(scpStat);

    this.add(rpanel, BorderLayout.EAST);
    this.add(lpanel, BorderLayout.WEST);

    rpanel.setLayout(new BoxLayout(rpanel, BoxLayout.Y_AXIS));
    lpanel.setLayout(new BoxLayout(lpanel, BoxLayout.Y_AXIS));


    rpanel.add(scpo);
    rpanel.add(scpi);
    rpanel.add(bChatSend);
    for (JLabel label : labels) {
      label.setFont(new Font("SansSerif", Font.PLAIN, 20));
      label.setForeground(Color.green);
      label.setAlignmentX(CENTER_ALIGNMENT);
    }

    lpanel.setPreferredSize(new Dimension(250, 768));
    rpanel.setPreferredSize(new Dimension(250, 768));

    plist.setAlignmentX(Component.CENTER_ALIGNMENT);
    scpo.setAlignmentX(Component.CENTER_ALIGNMENT);
    scpi.setAlignmentX(Component.CENTER_ALIGNMENT);
    bChatSend.setAlignmentX(Component.CENTER_ALIGNMENT);

    chatOut.setBorder(BorderFactory.createLineBorder(Color.green));
    stats.setBorder(BorderFactory.createLineBorder(Color.green));
    chatIn.setBorder(BorderFactory.createLineBorder(Color.green));
    chatOut.setEditable(false);
    stats.setEditable(false);
    chatOut.setText("Welcome to Dancrawler");

    this.gp = new GamePanel(client);
    this.add(gp, BorderLayout.CENTER);

    this.getRootPane().getActionMap().put("fullscreen", new FullScreenAction());
    this.getRootPane().getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0, false), "fullscreen");

  }

  @Override
  public void worldUpdate() {
  }

  @Override
  public void gameEnded() {
    this.setVisible(false);
    this.dispose();
  }

  @Override
  public void playerUpdate() {
    plist.setVisibleRowCount(4);
    DefaultListModel<String> m = (DefaultListModel) plist.getModel();
    m.clear();
    for (String s : client.getPlayers()) {
      m.addElement(s);
    }
  }
}
